<template>
  <PageContainer>
    <Header title="管理员" />
    <view class="hb-p-4">
      <view class="hb-bg-white hb-p-3 hb-rounded-xl hb-box-shadow-base">
        <TmText label="管理员" _class="hb-font-bold hb-mb-2" :font-size="28" />
        <TmText
          label="管理员可以协助一起管理智能体、管理知识库等"
          color="#999996"
          :font-size="24"
        />
        <Paglist v-model="dataList" :is-show-loadmore="false" @register="register">
          <template v-for="item in dataList" :key="item.id">
            <DataItem :data="item" @delete="showDelModal(item.id)" @setting="handleSetting(item)" />
          </template>
        </Paglist>
      </view>
      <TmModal
        v-model:show="modalVisible"
        title="删除管理员"
        :height="300"
        :follow-theme="false"
        cancel-text="取消"
        cancel-color="#969799"
        ok-color="#EA5E0E"
        ok-text="确认删除"
        :ok-close="false"
        @ok="handleDelete"
      >
        <view class="hb-text-sm hb-text-[#333] hb-text-center">
          <text>确定要删除管理员“</text>
          <text class="hb-text-primary">{{ getItem?.userName }}</text>
          <text>”吗？删除后，该用户将无法再协助你管理</text>
        </view>
      </TmModal>

      <BottomSubmit
        :hide-cancel-btn="false"
        cancel-btn-label="手动添加管理员"
        submit-btn-label="邀请管理员"
        submit-btn-open-type="share"
        @cancel="handleAddAdmin"
      />
    </view>
  </PageContainer>
</template>

<script lang="ts" setup>
  /* assistantManage 智能助手管理员 */
  import DataItem from './components/DataItem.vue';
  import { Toast } from '@/utils';
  import { BotAdminPageListResult } from '@/api/aiSettings/types';
  import { useConList } from '@/components/Paglist/useConList';
  import { assistantInvitationCode, botAdminDel, botAdminPageList } from '@/api/aiSettings';
  import { getUrl } from '@/router';
  import { useUserStore } from '@/store/modules/user';

  const dataList = ref<BotAdminPageListResult[]>([]);
  const [register, { refresh }] = useConList();
  provide('api', botAdminPageList);

  const modalVisible = ref<boolean>(false);
  const invitationCode = ref<string>('');

  const getCode = () => assistantInvitationCode().then((res) => (invitationCode.value = res));

  customOnload(() => {
    uni.hideShareMenu({
      hideShareItems: ['shareAppMessage', 'shareTimeline'],
    });
    getCode();
  });

  const selected = ref<number>();
  const getItem = computed(() => dataList.value.find((item) => item.id === selected.value));

  const showDelModal = (id: number) => {
    selected.value = id;
    modalVisible.value = true;
  };

  const handleSetting = (data: BotAdminPageListResult) => {
    navigateTo({
      name: 'assistantSetting',
      options: {
        data,
        success: () => refresh(),
      },
    });
  };
  const handleDelete = async () => {
    await botAdminDel({ id: selected.value! });
    modalVisible.value = false;
    if (getItem.value?.phone === useUserStore().userInfo?.phone) {
      useUserStore().wxLogin();
      Toast({
        title: '管理员已移除',
        success: () => {
          reLaunch({
            name: 'aiSwitch',
          });
        },
      });
    } else {
      refresh();
      Toast('管理员已移除');
    }
  };

  const handleAddAdmin = () => {
    navigateTo({
      name: 'assistantSetting',
      options: {
        success: () => refresh(),
      },
    });
  };

  onShareAppMessage((e) => {
    const code = invitationCode.value;
    getCode();
    return {
      title: '邀请你一起管理智能助手',
      path: getUrl('assistantInvite', { invitationCode: code }),
      imageUrl: 'https://hb-saas.obs.cn-south-1.myhuaweicloud.com/saas-ai/share-img.png',
    };
  });
</script>

<style lang="scss" scoped></style>
